package com.dxx.D201910312055.dxx;


import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ServerHandshake;

import static java.lang.System.out;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dxx.D201910312055.MainActivity;

/**
 * This example demonstrates how to create a websocket connection to a server. Only the most important callbacks are overloaded.
 */
public class ExampleClient extends WebSocketClient {

    private static ExampleClient exampleClient = null;
    private static String getMessage = "";
    private static boolean isLogin = false;
//    String URL = "ws://192.168.0.106:1234/repeater";


    public ExampleClient(URI serverUri, Draft draft) {
        super(serverUri, draft);


    }

    public ExampleClient(URI serverURI) {
        super(serverURI);


    }

    public ExampleClient(URI serverUri, Map<String, String> httpHeaders) {
        super(serverUri, httpHeaders);


    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {

        System.out.println("opened connection");
    }

    @Override
    public void onMessage(String message) {
        getMessage = message;
//        out.println(message);

    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        // The codecodes are documented in class org.java_websocket.framing.CloseFrame
        out.println("Connection closed by " + (remote ? "remote peer" : "us") + " Code: " + code + " Reason: " + reason);
    }

    @Override
    public void onError(Exception ex) {
        ex.printStackTrace();
        // if the error is fatal then onClose will be called additionally
    }


    public static ExampleClient initExampleClient(String URL) {

        try {
            exampleClient = new ExampleClient(new URI(URL));
            exampleClient.connect();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }


        return exampleClient;
    }


    public static boolean sendExampleClient(String send) {

        if (exampleClient == null) {
            return false;
        }


        if (exampleClient.getReadyState().equals(WebSocket.READYSTATE.OPEN)) {
            exampleClient.send(send);
            return true;
        }

        return false;
    }

    //发送特定信息，一秒内，判断特定返回信息 成功返回true ,否则返回false
    public boolean Login(String send, String isMessage) {

        this.send(send);
        long currentTimeMillisLong = System.currentTimeMillis() + 1000;

        while (System.currentTimeMillis() < currentTimeMillisLong) {

            if (getMessage.equals(isMessage)) {
                return true;

            }
        }


        return false;
    }

    //链接服务器，一秒内 成功返回true...
    public boolean isConnect() {
        this.connect();
        long currentTimeMillisLong = System.currentTimeMillis() + 1000;


        while (System.currentTimeMillis() < currentTimeMillisLong) {

            if (this.getReadyState().equals(WebSocket.READYSTATE.OPEN)) {

                return true;

            }
        }


        return false;
    }


    //发送信息，一秒内返回返回服务器信息，否则返回"",要确保执行过程没有其他服务器操作
    public String getMessage(String m) {
        String message = getMessage;

        this.send(m);
        long currentTimeMillisLong = System.currentTimeMillis() + 1000;


        while (System.currentTimeMillis() < currentTimeMillisLong) {

            if (!message.equals(getMessage)) {

                return getMessage;

            }
        }

        return "";
    }

    /*
    public static void main(String[] args) throws URISyntaxException {

        new Thread(new Runnable() {
            public void run() {


                try {
                    ExampleClient.initExampleClient("ws://192.168.0.106:1234/repeater");
                    Thread.sleep(3000);
                    out.println(ExampleClient.sendExampleClient("setlogin:220"));
                    ExampleClient.sendExampleClient("setPassword:440");
                    ExampleClient.sendExampleClient("destroy");
//                    ExampleClient.sendExampleClient("com.jm.video");
                    Thread.sleep(1000);
                    out.println(getMessage);
                    if (getMessage.equals("true")){
                        out.println(111);
                    }

//                    JSONArray arr = JSON.parseArray(getMessage);

//                    String s = arr.getString(1);
//                    out.println(s);
//                    ExampleClient.sendExampleClient("com.jm.video");
//        c.send("setlogin:220");
//                    c.send("setPassword:440");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

     */
}